import http from '../http'
import request from "../request";

/**
 *  @parms resquest 请求地址 例如：http://197.82.15.15:8088/request/...
 *  @param '/testIp'代表vue-cil中config，index.js中配置的代理
 */
let resquest = "/api"

// get请求
export function getListAPI(params){
    return http.get(`${resquest}/getList.json`,params)
}
// post请求
export function postFormAPI(params){
    return http.post(`${resquest}/postForm.json`,params)
}
// put 请求
export function putSomeAPI(params){
    return http.put(`${resquest}/putSome.json`,params)
}
// delete 请求
export function deleteListAPI(params){
    return http.delete(`${resquest}/deleteList.json`,params)
}


/**
 * 注册用户
 * @param username 用户名
 * @param password 密码
 * @returns {Promise<unknown>}
 */
export function registerUser(username,password){
    console.log(username,password)
    // return request.postURL('/register',{username,password})
    return new Promise(function(resolve, reject){
        resolve(
            {
                code: 100,
                message: '用户注册成功',
                data: {}
            }
        );
    });
}


/**
 * 用户登录
 * @param username
 * @param password
 * @returns {Promise<unknown>}
 */
export function loginUser(username,password){
    console.log(username,password)
    // return request.postURL('/login',{username,password})
    return new Promise(function(resolve, reject){
            resolve(
                {
                        code: 100,
                        message: '用户登陆成功',
                        data: {
                            id: 1,
                            username: 'user',
                            url: 'https://img01.yzcdn.cn/vant/cat.jpeg'
                        }
                    }
            );
    });
}


/**
 *      {
 *          name: '张娟',
 *          description: '我爱张娟',
 *          userId: 001
 *       }
 * @param username
 * @param password
 * @returns {Promise<unknown>}
 */
export function toSay(name,desc,userId){
    console.log('开始请求')
    console.log(name,desc,userId)
    // return request.postURL('/toSay',{name,desc,userId})
    return new Promise(function(resolve, reject){
        resolve(
            {
                code: 100,
                message: '用户留言成功',
                data: {name,desc,userId}
            }
        );
    });
}

/**
 * 查看某个人留言板下的全部留言
 * @param name
 */
export function searchName(name){
    console.log('查找',name,'下面的所有留言记录')
    return request.postURL('/search-name',{name})
}

/**
 * 查看某个人留言板下的具体留言
 * @param name
 * @pathParam id
 */
export function searchNameById(id,name){
    console.log('查找',name,'下面的所有留言记录')
    return request.postURL('/search-name/'+id,{name})

}
function unique(arr) {
    if (!Array.isArray(arr)) {
        console.log('type error!')
        return
    }
    var array =[];
    for(var i = 0; i < arr.length; i++) {
        if( !array.includes( arr[i]) ) {//includes 检测数组是否有某个值
            array.push(arr[i]);
        }
    }
    return array
}
/**
 * 查看某个人留言板下的全部名单
 * @param id
 */
export function getNames(id){
    // return request.postURL('/getNames',{id})
    let localMySayList = JSON.parse(localStorage.getItem('mySayList'))
    let list = []
    if(localMySayList==null){
        localMySayList = []
    }
    console.log('start',localMySayList)
    localMySayList.forEach((item,index)=>{
        console.log(item)
        if(item.userId == id){

            list.push(item.name)

        }
    })
    list = unique(list);

    console.log('end')
    return new Promise(function(resolve, reject){
        resolve(
            {
                code: 100,
                message: '用户留言成功',
                data: {
                    list
                }
            }
        );
    });
}

/**
 * 删除某人全部
 * @param name
 * @param id
 */
export function deleteAllSay(name,id){
    // return request.postURL('/deleteAllSay',{name,id})

    const localMySayList = JSON.parse(localStorage.getItem('mySayList'))
    const arr = Array(localMySayList)
    console.log('delete start',localMySayList)
    arr.forEach((item,index)=>{
        console.log(item)
        if(item.userId == id && item.name == name){
            console.log(index)
            // delete localMySayList[index]
            // localMySayList.split(index,1)

            arr.splice(index,1)
        }
    })

    console.log('delete end',arr)
    localStorage.setItem('mySayList',JSON.stringify(arr))
    return new Promise(function(resolve, reject){
        resolve(
            {
                code: 100,
                message: '删除成功',
                data: {}
            }
        );
    });
}

/**
 * 删除某人单个信息
 * @param name
 * @param id
 * @param description
 */
export function deleteOneSay(name,description,userId){
    return request.postURL('/deleteOneSay',{name,description,userId})
}

